<?php
$this->breadcrumbs = array(
    '数据批量处理' => array('index'),
    '数据导入',
);
?>
<div class="box corners shadow">
    <div class="box-header">
        <h2>数据导入</h2>
        <div class="box-header-ctrls">	
            <a href="javascript:void(null);" title="" class="close"><!-- --></a>
        </div>
    </div>

    <div class="box-content" id="contacts-1a">
        <div class="inbox-sf2">
            <form method="get" action="<?php echo $this->createUrl('excel/exportExcel'); ?>" target="_blank" onsubmit="return checkForm(this)">
                <?php
                echo CHtml::dropDownList('product_model', '', array('' => '选择数据模型') + Yii::app()->params->model_array + array('GAME_SERVER' => '游戏服务区'), array('class' => 'form-input', 'title' => '选择数据模型', 'rel-required' => 'true', 'rel-message' => '选择数据模型'));
                echo CHtml::dropDownList('game', '', array('' => '选择游戏') + ProductCategory::model()->getGameSelect(), array('class' => 'form-input', 'title' => '选择游戏', 'rel-required' => 'true', 'rel-message' => '请选择游戏'));
                ?>
                <input type="submit" name="" value="导出模板" class="inbox-sf-search-btn" />
                <a class="button black fr" href="<?php echo $this->createUrl('exportData');?>">
                    <span>导出数据</span>
                </a>
            </form>
        </div>

        <div class="inbox-sf2" style="background-color: #FFC;">
            <form method="post" action="<?php echo $this->createUrl('excel/importData'); ?>" enctype="multipart/form-data" onsubmit="return checkForm(this)">
                <?php
                echo CHtml::dropDownList('product_model', '', array('' => '选择数据模型') + Yii::app()->params->model_array + array('GAME_SERVER' => '游戏服务区'), array('class' => 'form-input', 'title' => '选择数据模型', 'rel-required' => 'true', 'rel-message' => '选择数据模型', 'id' => 'product_model2', 'onchange' => 'getSkill(this)'));


                echo CHtml::dropDownList('game', '', array('' => '选择游戏') + ProductCategory::model()->getGameSelect(), array('class' => 'form-input', 'title' => '选择游戏', 'rel-required' => 'true', 'rel-message' => '请选择游戏', 'onchange' => 'getSkill(this)', 'id' => 'game2'));
                ?>
                <div style="float:left;" id="skill_box">

                </div>
                <input class="form-input" rel-required="true" rel-message="请选择文件" type="file" name="leadExcel" title="选择文件" />
                <input type="submit" name="" value="导入" class="inbox-sf-search-btn" />

            </form>
        </div>

        <?php
        //数据导入信息
        if (isset($_POST['game'])):
            ?>
            <div>
                <div style="width: 19%;float:left;margin-left: 20px;border-right: solid 1px #ddd;">
                    <ol style="font-size: 14px;font-weight: bold;font-family: 微软雅黑">
                        <li>导入数量：<b id="in_count"><?php echo $rMsg['i_count']; ?></b></li>
                        <li>相同数据量：<B><?php echo $eModel->allEqNum; ?></B></li>
                        <li>总数量：<b><?php echo $rMsg['a_count'] - 1; ?></b></li>
                        <li>产品：<?php echo Yii::app()->params->model_array[$type]; ?></li>
                        <li>游戏：<?php echo ProductCategory::model()->getGameNameById($game); ?></li>
                        <?php
                        if ($eModel->skillId > 0) {
                            echo "<li>技能：" . ProductSkill::model()->findByPk($eModel->skillId)->skill_name . "</li>";
                        }
                        ?>
                        <li>网站：<?php echo $website; ?></li>
                    </ol>
                </div>
                <div style="width: 78%;float: right;">
                    <div style="margin-bottom: 10px;">
                        <span style="color:red">以下为重复数据,请注意处理 ↓ </span>
                        <a href="javaScript:void(0)" onclick="updateAll()" >[全部更新]</a>
                    </div>
                    <div style="padding: 0px 5px;" class="grid-view">
                        <!-- 执行导入-->
                        <table id="repeat" class="tablebox" width="100%">
                            <?php echo $rMsg['msg']; ?>
                        </table>
                    </div>
                </div>
                <div class="clear"></div>
            </div>

            <?php
        endif;
        ?>

    </div>

</div>
<script>
    function checkForm(obj){
        var sub=false;
        $.each($(obj).find(".form-input"), function(i, n){
            if($(n).attr("rel-required") == 'true' && $(n).val() == ""){
                alert($(n).attr("rel-message"));
                sub = false;
                $(n).focus();
                return false;
            }else{
                sub = true;
            }
        });

        return sub;
    }
    
    //如果选择技能等级类型，输出技能选择框
    function getSkill(obj){
        if($(obj).val()!=""){
            if($("#product_model2").val() == "SKILL_LEVEL"){
                $.post("<?php echo $this->createUrl('getSkill'); ?>", {game_id:$("#game2").val()}, function(data){
                    $("#skill_box").html(data);
                });
            }else{
                $("#skill_box").html("");
            }
        }
    }
            
    function updateData(obj){
        var id = $(obj).attr('rel');
        var url = "<?php echo $this->createUrl('updateAjax'); ?>";
        var data = {};
        data.id = id;
        data.game = "<?php echo $game ?>";
        data.productModel= "<?php echo $type ?>";
        data.siteKey = "<?php echo $website ?>"; 
        var jData = $(obj).parent().parent().find('td');
        $.each(jData,function(n,o){
            var fieldName = $(o).attr('rel');
            if(fieldName!='sb_hbj'){
                data[fieldName] = $(o).html();
            }
        });
        $.post(url,data,function(data){
            if(data==1){
                var haveIn = parseInt($("#in_count").html())+1;
                $("#in_count").html(haveIn);
                $(obj).parent().html("<span style='color:green'>成功</span>");
            }else{
                $(obj).parent().html("<span style='color:red'>失败</span>");
            }
        });
    }
    /**
     * 更新所有
     */
    function updateAll(){
        var cObj = $('#repeat td').find('a.update_sb');
        $.each(cObj,function(n,o){
            updateData(o);
        });
    }
</script>